package io.nio;

import framework.AbstractTest;
import utils.Log;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by shane on 2018/5/20.
 *
 * nio的目标就是快速移动大量数据， 因此ByteBuffer的大小就显得尤为重要
 */
public class ChannelCopy extends AbstractTest {
    protected static final String TAG = ChannelCopy.class.getName();
    
    @Override
    public void test(String[] args) {
        super.test(args);

        String inPath = getDefInputFile(args);
        String outPath = getDefOutputFile(args);

        try {
            FileChannel in = new FileInputStream(inPath).getChannel();
            FileChannel out = new FileOutputStream(outPath).getChannel();

            ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
            while (in.read(buffer) != -1) {
                buffer.flip();
                out.write(buffer);
                buffer.clear();
            }

            in.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected String getTag() {
        return TAG;
    }

}
